package com.swm.client;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JDialog;

import com.swm.data.DataManager;

public class RestClientMain {

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub		
		DataManager.getInstance().setSessionID("shk");				
		//JDialog loginDlg = new LoginDialog();		
		JDialog loginDlg = new ClientDialog();				
		//testPost();
	}
	
	public static void testPost()
	{
		/*
		String orgMsg="abcdefg";
	    String encMsg = null;
	    String header1 = "header1";
	    String header2 = "header2";
	    String header3 = "header3";
	    String ContentsType="text/xml";
	    

	    AES256 aes = new AES256();
	    encMsg = aes.encrypt(msg);

	      String xmldata = "<? xml version=\"1.0\" encoding=\"euc-kr\"?> "
	                            +"<! DOCTYPE lgtpims:schema>"
	                            +"<schema targetns=\"http://www.aaa.com\"" 
	                    +"version=\"1.0\" xmlns=\"http://www.w3.org/2000/10/XMLSchema\">"
	                        +"<parameter method=\"add\">"
	                            +"<element-value name=\"addelement\">aaaaa</element-value>"
	                            +"<element-value name=\"addelementtype\">bbbbb</element-value>"
	                        +"</parameter>"
	                    +"</schema>" ;

	        String strURL = "http://www.aaa.com/aaa";
	        PostMethod post = new PostMethod(strURL);
	        post.setRequestHeader("Content-type", "text/xml; charset=ISO-8859-1");
	        post.setRequestHeader("header1",header1); 
	        post.setRequestHeader("header2",header2);
	        post.setRequestHeader("header3",header3);
	        post.setRequestHeader("Content-Length",Integer.toString(xmldata.length()));
	        post.setRequestBody(xmldata);

	        HttpClient httpclient = new HttpClient();            

	        try {
	            int result = httpclient.executeMethod(post);
	            // Display status code
	            out.println("<br>Response status code: " + result);
	            // Display response
	             out.println("<br>Response header: "+post.getResponseHeader("Status-Line"));
	            out.println("<br>Response body: ");
	            out.println(post.getResponseBodyAsString());
	        } finally {
	            // Release current connection to the connection pool once you are done
	            post.releaseConnection();
	        }
	        */
	}
	
	public static void test()
	{		 
		try {
			// Construct data
			String data = URLEncoder.encode("id", "UTF-8") + "=" + URLEncoder.encode("tdev_java", "UTF-8");
			data += "&" + URLEncoder.encode("passwd", "UTF-8") + "=" + URLEncoder.encode("789632", "UTF-8");

			// Create a socket to the host
			// String hostname = "wap2.samsungmobile.com";
			String hostname = "localhost";
			int port = 8080;
			InetAddress addr = InetAddress.getByName(hostname);
			Socket socket = new Socket(addr, port);

			// Send header
			// String path = "/test/tdev/LoginExec.jsp";
			String path = "";
			BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(
					socket.getOutputStream(), "UTF8"));

			// Get response
			BufferedReader rd = new BufferedReader(new InputStreamReader(socket
					.getInputStream()));

			// Write post message
			wr.write("POST " + path + " HTTP/1.0\r\n");
			wr.write("Content-Length: " + data.length() + "\r\n");
			wr.write("Content-Type: application/x-www-form-urlencoded\r\n");
			wr.write("\r\n");

			// Send data
			System.out.println("Write data...");
			wr.write(data);
			wr.flush();

			// Read response from server
			System.out.println("Read response...");
			String line;
			while ((line = rd.readLine()) != null)
				System.out.print(line);

			wr.close();
			rd.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	
}
